#include "image_loader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lvgl.h>
#include "esp_lcd_panel_io.h"

// 静态变量存储路径和配置
static const char *frame_path = NULL;
static int frame_width = 0;
static int frame_height = 0;
static int buffer_size = 0;
static int total_frames = 0;

// 初始化图像加载器
bool image_loader_init(const char *path, int width, int height, int buffer) {
    frame_path = path;
    frame_width = width;
    frame_height = height;
    buffer_size = buffer;

    // 获取总帧数
    lv_fs_dir_t dir;
    if (lv_fs_dir_open(&dir, frame_path) != LV_FS_RES_OK) {
        LV_LOG_ERROR("Failed to open directory: %s", frame_path);
        return false;
    }

    char filename[128];
    total_frames = 0;
    while (lv_fs_dir_read(&dir, filename) == LV_FS_RES_OK) {
        if (strlen(filename) == 0) break; // 读取完成
        total_frames++;
    }

    lv_fs_dir_close(&dir);
    LV_LOG_INFO("Total frames: %d", total_frames);
    return true;
}

// 获取文件夹中的总帧数
int image_loader_get_frame_count(void) {
    return total_frames;
}

// 加载帧到指定缓冲区
bool image_loader_load_frames(Queue_t *buffer, int start_frame) {
    size_t img_size = frame_width * frame_height * 2;

    for (int i = 0; i < buffer_size; i++) {
        int frame_index = (start_frame + i) % total_frames;

        // 分配帧内存
        uint8_t *frame_data = heap_caps_malloc(img_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
        if (!frame_data) {
            LV_LOG_ERROR("Failed to allocate buffer for frame %d", frame_index);
            return false;
        }

        // 构造文件路径
        char path[128];
        snprintf(path, sizeof(path), "%s/%03d.bin", frame_path, frame_index);

        // 打开文件
        lv_fs_file_t file;
        lv_fs_res_t ret = lv_fs_open(&file, path, LV_FS_MODE_RD);
        if (ret != LV_FS_RES_OK) {
            LV_LOG_ERROR("Failed to open file: %s, error: %d", path, ret);
            free(frame_data);
            return false;
        }

        // 读取文件数据
        if (lv_fs_read(&file, frame_data, img_size, NULL) != LV_FS_RES_OK) {
            LV_LOG_ERROR("Read error for frame %d", frame_index);
            lv_fs_close(&file);
            free(frame_data);
            return false;
        }

        lv_fs_close(&file);

        // 将帧数据加入队列
        if (!queue_enqueue(buffer, frame_data)) {
            LV_LOG_ERROR("Failed to enqueue frame %d", frame_index);
            free(frame_data);
            return false;
        }
    }

    LV_LOG_INFO("Frames %d to %d loaded into buffer", start_frame, (start_frame + buffer_size - 1) % total_frames);
    return true;
}

// 获取一个帧数据，并释放上一帧的内存
void *image_loader_get_frame_data(Queue_t *buffer) {
    static void *previous_frame_data = NULL;

    if (queue_is_empty(buffer)) {
        LV_LOG_ERROR("Buffer is empty, no frame data available");
        return NULL;
    }

    // 从队列中取出帧数据
    void *frame_data = queue_dequeue(buffer);
    if (!frame_data) {
        LV_LOG_ERROR("Failed to dequeue frame data");
        return NULL;
    }

    // 释放上一帧的内存
    if (previous_frame_data) {
        free(previous_frame_data);
        previous_frame_data = NULL;
    }

    // 保存当前帧数据为下一次的 previous_frame_data
    previous_frame_data = frame_data;

    // 返回当前帧数据
    return frame_data;
}

// 释放图像加载器的资源
void image_loader_deinit(void) {
    frame_path = NULL;
    frame_width = 0;
    frame_height = 0;
    buffer_size = 0;
    total_frames = 0;
    // 还需要完善清空缓冲区的逻辑
}
